-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-12997 - @OneToMany does not work with @JoinFormula #2547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HHH-12997 - @OneToMany does not work with @JoinFormula #2547
Conversation
…bined with @JoinFormula
| private String hierarchyPath; | ||
|
|
||
| @OneToMany(fetch = FetchType.LAZY) | ||
| @JoinFormula(value = "hierarchy_path like id || '/_%'", referencedColumnName = "hierarchy_path") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you annotate the inverse relation? I.e. private Invoice parentInvoice? Not sure whether even JoinColumn is allowed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that as well:
@ManyToOne(fetch = FetchType.LAZY)
@JoinFormula(value = "SUBSTR(hierarchy_path, 0, INSTR(hierarchy_path, '/') - 1)", referencedColumnName = "id")
private Invoice rootInvoice;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "rootInvoice")
private Set<Invoice> childInvoices;
but there is same exception as described here https://discourse.hibernate.org/t/how-to-use-the-hibernate-joinformula-annotation-with-a-onetomany-jpa-association/1461 (i.e. ManyToOne works with JoinFormula, but OneToMany doesn't - even when "mappedBy" is added and JoinFormula is on ManyToOne side).
I guess the question is just whether JoinFormula works with OneToMany - but if it doesn't, how is anyone supposed to know that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an existing test with a @JoinFormula @ManyToOne relation here: https://github.com/hibernate/hibernate-orm/blob/e5dc635a52362f69b69acb8d5b166b69b165dbbd/hibernate-core/src/test/java/org/hibernate/test/annotations/manytoonewithformula/ManyToOneWithFormulaTest.java . So what you're saying is that when this example is extended with an inverse mapping (a @OneToMany(mappedBy = ""), Hibernate fails to boot?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's exactly what I was trying to say. For instance, in the test that you mentioned, if you add following to Language entity:
private List<Message> messages;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "language")
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
the test fails with exception
Caused by: java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column
|
@vladmihalcea I would be interested in having your feedback on that. While I'm not sure this particular test case is correct, @jwgmeligmeyling has a point here: #2547 (comment) e.g. that a test does not work when we just add an inverse relationship. I'm wondering if it's even possible to support an inverse relationship with WDYT? |
|
@gsmet @jwgmeligmeyling I think it's a valid use case and it would be convenient to add support for it. |
|
I'm not sure we will ever implement this feature, but we should have a meaningful error, hence #9320. |
A test that shows that combining @JoinFormula with @onetomany fails during startup.